package com.life.view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.border.TitledBorder;

import com.life.constants.Constants;

public class BoardConfigurationPanel extends JPanel {

	WidgetRegistry widgetRegistry = WidgetRegistry.getInstance();
	boolean porportionalBoardValues = false;
	
	JSpinner boardWidthSpinner = null;
	JSpinner boardHeightSpinner = null;
	SpinnerNumberModel boardWidthSpinnerModel = null;
	SpinnerNumberModel boardHeightSpinnerModel = null;
	
	public BoardConfigurationPanel()
	{
		this.setLayout(null);
		
		JPanel boardSizePanel = new JPanel();
		TitledBorder boardTitle = new TitledBorder("Board Configuration");
		boardSizePanel.setBorder(boardTitle);
		boardSizePanel.setBounds(0,0,290,125);
		boardSizePanel.setLayout(null);
		
		
		JButton applyButton = new JButton("Apply");
		applyButton.setBounds(150,25,95,55);
	    boardSizePanel.add(applyButton);
	    
	    widgetRegistry.put(Constants.BOARD_WIDTH_WIDGET,boardWidthSpinner);
	    
	    boardWidthSpinner = new JSpinner();
	    boardWidthSpinnerModel = new SpinnerNumberModel();
	    boardWidthSpinnerModel.setMaximum(new Integer(Constants.MAX_BOARD_SIZE));
	    boardWidthSpinnerModel.setMinimum(new Integer(Constants.MIN_BOARD_SIZE));
	    boardWidthSpinnerModel.setStepSize(new Integer(Constants.BOARD_STEP));
	    boardWidthSpinnerModel.setValue(new Integer(Constants.START_BOARD_SIZE));
	    boardWidthSpinner.setModel(boardWidthSpinnerModel);
	    boardWidthSpinner.setBounds(45,25,75,25);
	    this.add(boardWidthSpinner);
	    
	    JLabel widthLabel = new JLabel("Width");
	    widthLabel.setBounds(10,30,40,10);
	    this.add(widthLabel);	    
	    
	    boardHeightSpinner = new JSpinner();
	    boardHeightSpinnerModel = new SpinnerNumberModel();
	    boardHeightSpinnerModel.setMaximum(new Integer(Constants.MAX_BOARD_SIZE));
	    boardHeightSpinnerModel.setMinimum(new Integer(Constants.MIN_BOARD_SIZE));
	    boardHeightSpinnerModel.setStepSize(new Integer(Constants.BOARD_STEP));
	    boardHeightSpinnerModel.setValue(new Integer(Constants.START_BOARD_SIZE));
	    boardHeightSpinner.setModel(boardHeightSpinnerModel);
	    boardHeightSpinner.setBounds(45,55,75,25);
	    this.add(boardHeightSpinner);
	    
	    JLabel heightLabel = new JLabel("Height");
	    heightLabel.setBounds(10,60,40,15);
	    this.add(heightLabel);
	    
	    JCheckBox keepPorportionalCB = new JCheckBox("Keep Porportional");
	    keepPorportionalCB.setBounds(10,100,150,15);
	    this.add(keepPorportionalCB);
	    
	    keepPorportionalCB.setSelected(false);
	    
	    keepPorportionalCB.addActionListener(new ActionListener(){
	    	
	    	public void actionPerformed(ActionEvent event)
	    	{
	    		if(porportionalBoardValues)
	    		{
	    			boardHeightSpinner.setModel(boardHeightSpinnerModel);
	    			boardWidthSpinner.setModel(boardWidthSpinnerModel);
	    			porportionalBoardValues = false;
	    		}
	    		else
	    		{
	    			boardHeightSpinner.setModel(boardWidthSpinnerModel);
	    			boardWidthSpinner.setModel(boardWidthSpinnerModel);
	    			porportionalBoardValues = true;
	    		}
	    	}
	    	
	    });
	    
	    JPanel colorOptionPanel = new NeighborColorPanel();
	    colorOptionPanel.setBounds(0,130,290,300);   
	    
	    this.add(boardSizePanel);
	    this.add(colorOptionPanel);
	    
		JLabel label0 = new JLabel("0");
		JLabel label1 = new JLabel("0");
		JLabel label2 = new JLabel("0");
		JLabel label3 = new JLabel("0");
		JLabel label4 = new JLabel("0");
		JLabel label5 = new JLabel("0");
		JLabel label6 = new JLabel("0");
		JLabel label7 = new JLabel("0");
		JLabel label8 = new JLabel("0");
		JLabel label9 = new JLabel("0");
		JLabel backgroundLabel = new JLabel("Background");
	    
		widgetRegistry.put(Constants.BOARD_WIDTH_WIDGET,boardWidthSpinner);
	    widgetRegistry.put(Constants.BOARD_HEIGHT_WIDGET,boardHeightSpinner);
	    widgetRegistry.put(Constants.BOARD_SIZE_APPLY_WIDGET,applyButton);
		
	}
}
